package com.canlihao.ui.mvp.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.canlihao.ui.mvp.callback.Callback;
import com.canlihao.ui.mvp.http.CommonConstant;
import com.canlihao.ui.utils.Tools;

import java.util.HashMap;
import java.util.Map;

/*******
 *
 *Created by panda李
 *
 *创建时间 2020/3/30
 *
 *类描述：
 *
 ********/
public class LoginModel extends BaseModel {
    @Override
    public void execute(final Callback<JSONObject> myCallBack) {
        if (Tools.isEmpty(mvpParams[0])) {
            String result = "{\"code\":501,\"message\":\"用户名不能为空\",\"result\":{},\"success\":false,\"timestamp\":0}";
            JSONObject resultJson = JSON.parseObject(result);
            myCallBack.onFailure(resultJson);
            myCallBack.onComplete();
            return;
        } else if (Tools.isEmpty(mvpParams[1])) {
            String result = "{\"code\":501,\"message\":\"密码不能为空\",\"result\":{},\"success\":false,\"timestamp\":0}";
            JSONObject resultJson = JSON.parseObject(result);
            myCallBack.onFailure(resultJson);
            myCallBack.onComplete();
            return;
        }
        //用户名、密码、以及约定数据
//        String url = CommonConstant.URL + "common/login" + "?username=" + mvpParams[0] +
//                "&password=" + DigestUtils.md5Hex(mvpParams[1]) +
//                "&roleId=" + mvpParams[2];

//        String url = "https://www.baidu.com";
//        //login接口JWT为空，返回会拿到一个JWT，JWT是放在ResponseBody里的，再下次请求时要把JWT放进RequestHeader
//        Map<String,Object> map = new HashMap<>();
//        Map<String,String> map1 = new HashMap<>();
//        map1.put("userName","ll");
//        map1.put("passWord","can");
//        map.put("login",map1);
//        requestPostAPI(url,map, myCallBack, "");
        if(!mvpParams[0].equals("123")){
            String result = "{\"code\":501,\"message\":\"用户名填写错误\",\"result\":{},\"success\":false,\"timestamp\":0}";
            JSONObject resultJson = JSON.parseObject(result);
            myCallBack.onFailure(resultJson);
            myCallBack.onComplete();
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    String result = "{\"code\":0,\"message\":\"登录成功\",\"data\":{\"userNo\":\"us2054061490822416\",\"userHead\":\"https://static.ss-clouddoctor.com/data/20200327/7c7e79f7e3734ba5b3a4925d34076384.jpg\",\"userName\":\"不期而至！\",\"nickName\":\"不期而至！\",\"userSex\":1,\"rongToken\":\"bqBzli68uG+zDY0XgPgGHouE4h8tIsS+EVPIIs3eWW8tOlgdhLSrJ0IF9fs+iO6PhznMzOQVszQqIQSBA2nEAkJAkb3Ubm0wifaJvSXdW3E=\",\"sysToken\":\"aa7dfdbf50064d808e0b1a2304400570\",\"platformType\":\"wechat\",\"phoneNo\":\"15680908732\"}}";
                    JSONObject resultJson = JSON.parseObject(result);
                    myCallBack.onSuccess(resultJson);
                    myCallBack.onComplete();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
